#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    bool vis[101][101];
    int m, n;
    int nearestExit(vector<vector<char>>& maze, vector<int>& e) {
        m = maze.size(); n = maze[0].size();
        queue<pair<int, int>> q;
        q.push({ e[0],e[1] });
        vis[e[0]][e[1]] = true;
        int ret = 0;
        while (q.size())
        {
            ret++;
            int sz = q.size();
            for (int i = 0; i < sz; i++)
            {
                auto [a, b] = q.front();
                q.pop();
                for (int k = 0; k < 4; k++)
                {
                    int x = dx[k] + a; int y = dy[k] + b;
                    if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && maze[x][y] == '.')
                    {
                        if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ret;
                        q.push({ x,y });
                        vis[x][y] = true;

                    }
                }
            }
        }
        return -1;
    }
};